﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Logging;

namespace MMITIntegration
{
    public class UkashInput
    {
        private string ukashLogin = String.Empty;

        public string UkashLogin
        {
            get { return ukashLogin; }
            set { ukashLogin = value; }
        }
        private string ukashPassword = String.Empty;

        public string UkashPassword
        {
            get { return ukashPassword; }
            set { ukashPassword = value; }
        }
        private string ukashTransactionID = String.Empty;

        public string UkashTransactionID
        {
            get { return ukashTransactionID; }
            set { ukashTransactionID = value; }
        }
        private string ukashBrandID = String.Empty;

        public string UkashBrandID
        {
            get { return ukashBrandID; }
            set { ukashBrandID = value; }
        }
        private string ukashVoucherNumber = String.Empty;

        public string UkashVoucherNumber
        {
            get { return ukashVoucherNumber; }
            set { ukashVoucherNumber = value; }
        }
        private string ukashVoucherValue = String.Empty;

        public string UkashVoucherValue
        {
            get { return ukashVoucherValue; }
            set { ukashVoucherValue = value; }
        }
        private string ukashBaseCurrency = String.Empty;

        public string UkashBaseCurrency
        {
            get { return ukashBaseCurrency; }
            set { ukashBaseCurrency = value; }
        }
        private string ukashTicketValue = String.Empty;

        public string UkashTicketValue
        {
            get { return ukashTicketValue; }
            set { ukashTicketValue = value; }
        }
        private string ukashRedemptionType = String.Empty;

        public string UkashRedemptionType
        {
            get { return ukashRedemptionType; }
            set { ukashRedemptionType = value; }
        }
        private string ukashMerchantDateTime = String.Empty;

        public string UkashMerchantDateTime
        {
            get { return ukashMerchantDateTime; }
            set { ukashMerchantDateTime = value; }
        }
        private string ukashMerchantCustomerValue = String.Empty;

        public string UkashMerchantCustomerValue
        {
            get { return ukashMerchantCustomerValue; }
            set { ukashMerchantCustomerValue = value; }
        }
        private string ukashStoreLocationId = String.Empty;

        public string UkashStoreLocationId
        {
            get { return ukashStoreLocationId; }
            set { ukashStoreLocationId = value; }
        }
        private string ukashAmountReference = String.Empty;

        public string UkashAmountReference
        {
            get { return ukashAmountReference; }
            set { ukashAmountReference = value; }
        }

        public override String ToString()
        {
            string outputXml = "";

            outputXml = string.Format("<UKashTransaction><ukashLogin>{0}</ukashLogin><ukashPassword>{1}</ukashPassword><transactionId>{2}</transactionId><brandId>{3}</brandId><voucherNumber>{4}</voucherNumber><voucherValue>{5}</voucherValue><baseCurr>{6}</baseCurr><ticketValue>{7}</ticketValue><redemptionType>{8}</redemptionType><merchDateTime>{9}</merchDateTime><merchCustomValue>{10}</merchCustomValue><storeLocationId>{11}</storeLocationId><amountReference>{12}</amountReference></UKashTransaction>",
                UkashLogin,
                UkashPassword,
                UkashTransactionID,
                UkashBrandID,
                UkashVoucherNumber,
                UkashVoucherValue,
                UkashBaseCurrency,
                UkashTicketValue,
                UkashRedemptionType,
                UkashMerchantDateTime,
                UkashMerchantCustomerValue,
                UkashStoreLocationId,
                ukashAmountReference
                );

            //Logging XML to UKASH
            Dictionary<string, object> d = new Dictionary<string, object>();
            SynkronLogger Log = new SynkronLogger("MMITCore");

            d.Add("XML sent to UKASH", outputXml);
            Log.WriteInformation(56421723, "XML sent to UKASH", d);


            return outputXml;
        }

        public static UkashInput FromString(string inputXml)
        {
            UkashInput bean = new UkashInput();

            // assume input element is an XML, in other case an exception will be thrown and captured
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(inputXml);


            // iterate over nodes in the document assuming they are children on the first child
            foreach (XmlNode node in doc.FirstChild.ChildNodes)
            {
                switch (node.Name)
                {
                    case "ukashLogin":
                        bean.UkashLogin = node.InnerText;
                        break;

                    case "ukashPassword":
                        bean.UkashPassword = node.InnerText;
                        break;

                    case "transactionId":
                        bean.UkashTransactionID = node.InnerText;
                        break;

                    case "brandId":
                        bean.UkashBrandID = node.InnerText;
                        break;

                    case "voucherNumber":
                        bean.UkashVoucherNumber = node.InnerText;
                        break;

                    case "voucherValue":
                        bean.UkashVoucherValue = node.InnerText;
                        break;

                    case "baseCurr":
                        bean.UkashBaseCurrency = node.InnerText;
                        break;

                    case "ticketValue":
                        bean.UkashTicketValue = node.InnerText;
                        break;

                    case "redemptionType":
                        bean.UkashRedemptionType = node.InnerText;
                        break;

                    case "merchDateTime":
                        bean.UkashMerchantDateTime = node.InnerText;
                        break;

                    case "merchCustomValue":
                        bean.UkashMerchantCustomerValue = node.InnerText;
                        break;

                    case "storeLocationId":
                        bean.UkashStoreLocationId = node.InnerText;
                        break;

                    case "amountReference":
                        bean.UkashAmountReference = node.InnerText;
                        break;
                }
            }
          
            return bean;
        }
    }
}